
import { ICapability } from "@/components/capability";
import { server } from "@/views/Development/server";
import { message } from "antd";
import React from "react";
import { v4 as uuidv4 } from 'uuid';
import { MenuController } from "@/controllers/MenuController";

/** 保存菜单 */
export function useSaveMenu(): ICapability {

    const [loading, setLoading] = React.useState(false);

    const controller = React.useContext(MenuController.Context);

    const menu = controller.useMemo(s => s.menu);

    const onStart = React.useCallback(async () => {
        setLoading(true);
        try {
            message.success('保存成功');
            server('/dev/menu/add', { method: 'post', body: JSON.stringify(menu, null, 4) });
        } catch (error) {
            console.error(error);
        }
        setLoading(false);
    }, [menu])

    return {
        id: uuidv4(),
        title: '保存',
        onStart,
        loading,
    }
}